package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityItemService activityItemService;


    @RequiredPermission(name="活动页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/投票列表";
    }


    @RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity",activityService.get(id));
        }
        return "activity/input";
    }

    @RequiredPermission(name="活动投票",expression="activity:vote")
    @RequestMapping("/vote")
    public String vote(Long id, Model model) {
        Activity activity = activityService.get(id);
        model.addAttribute("activity",activity);

        List<ActivityItem> items = activityItemService.selectByActivityId(id);
        model.addAttribute("items",items);
        return "activity/投票页面";
    }
    @RequiredPermission(name="投票结果",expression="activity:changeResult")
    @RequestMapping("/changeResult")
    public String changeResult(Long id, Long activityId, Model model) {
        Activity activity = activityService.get(activityId);
        model.addAttribute("activity",activity);

        //改变数据 (id)
        activityItemService.updateResult(id);
        //记录
        activityItemService.recordResult(UserContext.getCurrentUser().getId(),id,new Date());
        //查总投票数
        Integer totalNum = activityItemService.selectTotalNum(activityId);
        model.addAttribute("totalNum",totalNum);

        List<ActivityItem> items = activityItemService.selectByActivityId(activityId);
        model.addAttribute("items",items);

        return "activity/投票结果";
    }
}
